package com.ruoyi.fpa.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.util.SnowFlakeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.fpa.domain.GenReferenceColumn;
import com.ruoyi.fpa.mapper.GenReferenceMapper;
import com.ruoyi.fpa.domain.GenReference;
import com.ruoyi.fpa.service.IGenReferenceService;

/**
 * 外键Service业务层处理
 *
 * @author koradji@116.com
 * @date 2023-09-17
 */
@Service
public class GenReferenceServiceImpl implements IGenReferenceService
{
    @Autowired
    private GenReferenceMapper genReferenceMapper;

    /**
     * 查询外键
     *
     * @param referenceId 外键主键
     * @return 外键
     */
    @Override
    public GenReference selectGenReferenceByReferenceId(Long referenceId)
    {
        return genReferenceMapper.selectGenReferenceByReferenceId(referenceId);
    }

    /**
     * 查询外键列表
     *
     * @param genReference 外键
     * @return 外键
     */
    @Override
    public List<GenReference> selectGenReferenceList(GenReference genReference)
    {
        return genReferenceMapper.selectGenReferenceList(genReference);
    }

    /**
     * 新增外键
     *
     * @param genReference 外键
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGenReference(GenReference genReference)
    {
        genReference.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isEmpty(String.valueOf(genReference.getReferenceId()))) {
            genReference.setReferenceId(SnowFlakeUtils.snowFlakeId());
        }
        int rows = genReferenceMapper.insertGenReference(genReference);
        insertGenReferenceColumn(genReference);
        return rows;
    }

    /**
     * 修改外键
     *
     * @param genReference 外键
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGenReference(GenReference genReference)
    {
        genReference.setUpdateTime(DateUtils.getNowDate());
        genReferenceMapper.deleteGenReferenceColumnByReferenceId(genReference.getReferenceId());
        insertGenReferenceColumn(genReference);
        return genReferenceMapper.updateGenReference(genReference);
    }

    /**
     * 批量删除外键
     *
     * @param referenceIds 需要删除的外键主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGenReferenceByReferenceIds(Long[] referenceIds)
    {
        genReferenceMapper.deleteGenReferenceColumnByReferenceIds(referenceIds);
        return genReferenceMapper.deleteGenReferenceByReferenceIds(referenceIds);
    }

    /**
     * 删除外键信息
     *
     * @param referenceId 外键主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGenReferenceByReferenceId(Long referenceId)
    {
        genReferenceMapper.deleteGenReferenceColumnByReferenceId(referenceId);
        return genReferenceMapper.deleteGenReferenceByReferenceId(referenceId);
    }

    /**
     * 新增外键关联信息
     *
     * @param genReference 外键对象
     */
    public void insertGenReferenceColumn(GenReference genReference)
    {
        List<GenReferenceColumn> genReferenceColumnList = genReference.getGenReferenceColumnList();
        Long referenceId = genReference.getReferenceId();
        if (StringUtils.isNotNull(genReferenceColumnList))
        {
            List<GenReferenceColumn> list = new ArrayList<GenReferenceColumn>();
            for (GenReferenceColumn genReferenceColumn : genReferenceColumnList)
            {
                genReferenceColumn.setReferenceId(referenceId);
                if(StringUtils.isEmpty(String.valueOf(genReferenceColumn.getReferenceColumnId()))) {
                    genReferenceColumn.setReferenceColumnId(SnowFlakeUtils.snowFlakeId());
                }
                list.add(genReferenceColumn);
            }
            if (list.size() > 0)
            {
                genReferenceMapper.batchGenReferenceColumn(list);
            }
        }
    }
}
